//
//  MTRegisterViewController.m
//  MeToo
//
//  Created by 俊松何 on 2018/7/5.
//  Copyright © 2018年 tianxie. All rights reserved.
//

#import "MTRegisterViewController.h"
#import "MLLinkLabel.h"
#import "LTcountryCodeSelcectController.h"
#import "MTValidationCodeController.h"
#import "MTLoginViewController.h"
#import "MTWebViewController.h"

@interface MTRegisterViewController ()
@property (weak, nonatomic) IBOutlet UILabel *titleLable;
@property (weak, nonatomic) IBOutlet UILabel *contoryCodeLable;
@property (weak, nonatomic) IBOutlet UIButton *sureButton;

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property(nonatomic,copy) NSString *contryCode;


@property(nonatomic,strong) MLLinkLabel *protoclLabel;
@end

@implementation MTRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.contryCode = @"+86";
    self.titleLable.text = NSLTLocalizedString(@"register.inputphone", nil);
    self.phoneTextField.placeholder = NSLTLocalizedString(@"register.inputphone", nil);
    [self.sureButton setTitle:NSLTLocalizedString(@"main.sure", nil) forState:UIControlStateNormal];
    
    self.protoclLabel = [MLLinkLabel new];
    self.protoclLabel.textAlignment = NSTextAlignmentCenter;
    NSString* protocolStr = [NSString stringWithFormat:@"%@《%@》",NSLTLocalizedString(@"login.loginMeans", nil), NSLTLocalizedString(@"login.aggree", nil)];
    NSString* aggreementStr = [NSString stringWithFormat:@"《%@》", NSLTLocalizedString(@"login.aggree", nil)];
    self.protoclLabel.textColor = [UIColor lightGrayColor];
    self.protoclLabel.numberOfLines = 2;
    self.protoclLabel.font = [UIFont systemFontOfSize:12];
    NSMutableAttributedString* attString= [[NSMutableAttributedString alloc]initWithString:protocolStr];
    [attString setAttributes:@{NSForegroundColorAttributeName : UIColorFromRGB(0x94BD31), NSLinkAttributeName : @"asdsd"} range:[protocolStr rangeOfString:aggreementStr]];
    self.protoclLabel.attributedText = attString;
    @weakify(self);
    [self.protoclLabel setDidClickLinkBlock:^(MLLink *link, NSString *linkText, MLLinkLabel *label) {
        @strongify(self);
        [self protolClicked:nil];
    }];
    [self.view addSubview:self.protoclLabel];
    
    [self.protoclLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.sureButton.mas_left).offset(0);
        make.top.mas_equalTo(self.sureButton.mas_bottom).offset(20);
        make.right.mas_equalTo(self.sureButton.mas_right).offset(0);
    }];
    
    if(_validationCodeType == MTValidationCodeSetWalletPassword)
    {
        self.protoclLabel.hidden = YES;
    }
    
    
    
    RACSignal* textSigal =  [[self.phoneTextField rac_textSignal] map:^id _Nullable(NSString * _Nullable text) {
        if(text.length > 4)
        {
            return @(YES);
        }else
        {
            return  @(NO);
        }
    }];
    [textSigal subscribeNext:^(id  _Nullable x) {
        BOOL enable = [x boolValue];
        if(enable)
        {
            self.sureButton.backgroundColor = mainButtonColor ;
            [self.sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            self.sureButton.enabled = YES;
        }else
        {
            self.sureButton.backgroundColor = UIColorFromRGB(0xdfdfdf);
            [self.sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            self.sureButton.enabled = NO;
        }
    }];
    [self.phoneTextField becomeFirstResponder];
    
    [[MTVerCodeApiManager manager] getDefaultCountryCodeWithCompletion:^(MMCountry *code, MTError *mterror) {
        if(mterror == nil)
        {
            self.contoryCodeLable.text = code.dialCode;
            self.contryCode = code.dialCode;
        }
    }];
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)protolClicked:(UITapGestureRecognizer *)tap
{
    NSLog(@"protolClicked");
    MTWebViewController *webVc = [[MTWebViewController alloc] init];
    webVc.url = @"http://mitu_static.108usd.com/use-protocol";
    [self.navigationController pushViewController:webVc animated:YES];
    
}

- (IBAction)selectCodeClick:(UIButton *)sender {
    
    LTcountryCodeSelcectController *selectCodeVc = [[LTcountryCodeSelcectController alloc]  init];
    selectCodeVc.phoneblock = ^(NSString *name,NSString *code){
        NSLog(@"--%@",code);
        self.contryCode =  code;
        self.contoryCodeLable.text = self.contryCode;
    };
    [self.navigationController pushViewController:selectCodeVc animated:YES];
}

- (IBAction)sureClick:(UIButton *)sender {
    
    
//    [MTWalletApiManager manager]
    
    if(_validationCodeType == MTValidationCodeSetWalletPassword)
    {
        [MBProgressHUD showProcessHud:@"" toView:self.view];
        NSDictionary *param = @{@"countryCode":self.contryCode,@"phone":self.phoneTextField.text};
        [[MTHttpTool shareTool] getPath:@"/wallet/get_set_pas_verify_code" parameters:param success:^(MTBaseData *datamodel) {
            
            [MBProgressHUD hideHUDForView:self.view];
            MTValidationCodeController *verVc = [MTValidationCodeController baseStoryBordWithName:@"Login"];
            verVc.phone = self.phoneTextField.text;
            verVc.contryCode = self.contryCode;
            verVc.needRequstVerCode  = NO;
            verVc.validationCodeType = MTValidationCodeSetWalletPassword;
            [self.navigationController pushViewController:verVc animated:YES];
            
        } failer:^(MTError *error) {
             [MBProgressHUD showError:error.errorDescription toView:self.view];
        }];
    }else
    {
        [MBProgressHUD showProcessHud:@"" toView:self.view];
        [[MTUserApiManager manager] getregisterVerCode:self.phoneTextField.text countryCode:self.contryCode completion:^(MTError *mterror) {
            if(mterror == nil)
            {
                [MBProgressHUD hideHUDForView:self.view];
                MTValidationCodeController *verVc = [MTValidationCodeController baseStoryBordWithName:@"Login"];
                verVc.phone = self.phoneTextField.text;
                verVc.contryCode = self.contryCode;
                verVc.needRequstVerCode  = NO;
                verVc.validationCodeType = MTValidationCodeRegister;
                [self.navigationController pushViewController:verVc animated:YES];
            }else if (mterror.code == 2)
            {
                [MBProgressHUD hideHUDForView:self.view];
                UIAlertController *alterVc = [UIAlertController alertControllerWithTitle:NSLTLocalizedString(@"login.repeaRegister", nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *action1 = [UIAlertAction actionWithTitle:NSLTLocalizedString(@"main.cancle", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                }];
                UIAlertAction *action2 = [UIAlertAction actionWithTitle:NSLTLocalizedString(@"main.sure", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    MTLoginViewController *loginVc = [MTLoginViewController baseStoryBordWithName:@"Login"];
                    loginVc.phone = self.phoneTextField.text;
                    [self.navigationController pushViewController:loginVc animated:YES];
                }];
                [alterVc addAction:action1];
                [alterVc addAction:action2];
                [self presentViewController:alterVc animated:YES completion:nil];
            }
            else
            {
                [MBProgressHUD showError:mterror.errorDescription toView:self.view];
            }
        }];
        
        
        
        
    }
    
   
    
    
  
    
    
}

@end
